package com.duowei.diandan.db.entry;

import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;
import android.databinding.BaseObservable;
import android.databinding.Bindable;

import com.duowei.diandan.BR;
import com.duowei.diandan.app.SqlServerDateTime;
import com.duowei.diandan.sql.InsertField;
import com.duowei.diandan.sql.TableName;

import org.joda.time.LocalDateTime;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Locale;
import java.util.Objects;

/**
 * 点单临时表明细信息
 */
@TableName(name = "wmlsb")
@Entity
public class Wmlsb extends BaseObservable implements Serializable {
    @PrimaryKey(autoGenerate = true)
    private long id;

    /** 序号(主键) 不使用 */
    private Integer xh;
    /** 单据编号(关联wmlsbjb中的wmdbh) */
    @InsertField
    private String wmdbh;
    /** 单品编号 */
    @InsertField
    private String xmbh;
    /** 单品名称 */
    @InsertField
    private String xmmc;
    /** 单品内部编号 */
    @InsertField
    private String tm;
    /** 单位 */
    @InsertField
    private String dw;
    /** 数量 */
    @InsertField
    private Float sl;
    /** 售价 */
    @InsertField
    private Float dj;
    /** 小计（DJ*SL） */
    @InsertField
    private Float xj;
    /** 口味备注 */
    @InsertField
    private String pz;
    /** 点单时间戳 */
    @InsertField
    private String tcbh;
    /** 是否已下单（0未下单 1已下单） */
    @InsertField
    private String sfyxd;
    /** 是否时价单品 （1时价单品） */
    @InsertField
    private String xszt;
    @InsertField
    private Float ftje;
    /** 原价 */
    @InsertField
    private Float ysjg;
    /** 是否赠送 （1赠送） */
    @InsertField
    private String sfzs;
    /** 点单员姓名 */
    @InsertField
    private String syyxm;
    /** 特殊操作授权人姓名（如打折、赠送、修改单价） */
    @InsertField
    private String sqrxm;
    /** 赠送时间 */
    @InsertField
    private LocalDateTime zssj;
    @InsertField
    private Float dwsl;
    /** 是否显示（默认1）单品和套餐主项1，套餐子项显示0 */
    @InsertField
    private String sfxs;
    @InsertField
    private String by1;
    /** 单品类别编码 */
    @InsertField
    private String by2;
    /** 兑换该单品所需积分 */
    @InsertField
    private Float by3;
    @InsertField
    private Float by4;
    /** 点单时间 */
    @InsertField
    private LocalDateTime by5;
    @InsertField
    private String sjc;
    @InsertField
    private String by6;
    @InsertField
    private String by7;
    @InsertField
    private String by8;
    @InsertField
    private Float by9;
    @InsertField
    private Float by10;
    @InsertField
    private LocalDateTime by11;
    /** 套餐编号 */
    @InsertField
    private String tcxmbh;
    /** 否为称重产品 （0不是、1是） */
    @InsertField
    private String by12;
    /** 打折方式 */
    @InsertField
    private String by13;
    @InsertField
    private String pbjsjm;
    @InsertField
    private Integer pbxh;
    @InsertField
    private String by14;
    /** 套餐分组 */
    @InsertField
    private String by15;
    @InsertField
    private String by16;
    @InsertField
    private String by17;
    @InsertField
    private String by18;
    @InsertField
    private Integer by19;
    @InsertField
    private LocalDateTime by20;
    @InsertField
    private String by21;
    @InsertField
    private String by22;
    @InsertField
    private String by23;
    @InsertField
    private String by24;
    @InsertField
    private String by25;

    // 自定义字段

    // 是不是主项(单品，套餐都用)
    private boolean isMain = true;

    // 子项使用
    private long mainId;

    public Wmlsb() {
    }

    /**
     * 添加单品
     */
    public Wmlsb(Jyxmsz item, boolean isMain, long mainId) {
        this.xmbh = item.getXmbh();
        this.xmmc = item.getXmmc();
        this.dw = item.getDw();

        // 会员价处理
        Float memberPrice = item.getMemberPrice();
        if (memberPrice != null) {
            this.dj = memberPrice;
        } else {
            this.dj = item.getXsjg();
        }

        this.ysjg = item.getXsjg();
        this.sfxs = "1";
        this.tm = item.getTm();
        this.by2 = item.getLbbm();
        this.by3 = item.getYhj();

        this.sl = 1f;

        this.isMain = isMain;
        this.mainId = mainId;

        computeXj();
    }

    /**
     * 添加套餐
     *
     * @param item
     * @param sfxs 主项 1 子项 0
     * @param tcbh 当前时间
     */
    public Wmlsb(Tcsd item, String sfxs, String tcbh, boolean isMain, long mainId) {
        this.xmbh = item.getXmbh1();
        this.tm = item.getXmbh1();

        if ("1".equals(sfxs)) {
            this.xmmc = item.getXmmc1();
            this.by6 = "1";
        } else {
            this.xmmc = "  " + item.getXmmc1();
        }


        this.tcxmbh = item.getXmbh();
        this.dw = item.getDw1();
        this.dj = item.getDj();
        this.ysjg = item.getDj();
//        this.sfxs = sfxs;
        this.sfxs = "1";
        // 中西餐版套餐分组by15, 快餐版套餐分组12, 同时设置
        this.by12 = item.getTm();
        this.by15 = item.getTm();

        this.tcbh = tcbh;
        this.dwsl = item.getSl();
        this.by2 = item.getLbbm();
        this.by3 = item.getYhj();

        this.sl = item.getSl();
        this.by5 = SqlServerDateTime.getInstance().getNowServerDateTime();

        this.isMain = isMain;
        this.mainId = mainId;

        computeXj();
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Integer getXh() {
        return xh;
    }

    public void setXh(Integer xh) {
        this.xh = xh;
    }

    public String getWmdbh() {
        return wmdbh;
    }

    public void setWmdbh(String wmdbh) {
        this.wmdbh = wmdbh;
    }

    public String getXmbh() {
        return xmbh;
    }

    public void setXmbh(String xmbh) {
        this.xmbh = xmbh;
    }

    public String getXmmc() {
        return xmmc;
    }

    public void setXmmc(String xmmc) {
        this.xmmc = xmmc;
    }

    public String getTm() {
        return tm;
    }

    public void setTm(String tm) {
        this.tm = tm;
    }

    public String getDw() {
        return dw;
    }

    public void setDw(String dw) {
        this.dw = dw;
    }

    @Bindable
    public Float getSl() {
        return sl;
    }

    public void setSl(Float sl) {
        this.sl = sl;
        computeXj();

        notifyPropertyChanged(BR.sl);
    }

    public Float getDj() {
        return dj;
    }

    public void setDj(Float dj) {
        this.dj = dj;
        computeXj();
    }

    @Bindable
    public Float getXj() {
        return xj;
    }

    public void setXj(Float xj) {
        this.xj = xj;
        notifyPropertyChanged(BR.xj);
    }

    public String getPz() {
        return pz;
    }

    public void setPz(String pz) {
        this.pz = pz;
    }

    public String getTcbh() {
        return tcbh;
    }

    public void setTcbh(String tcbh) {
        this.tcbh = tcbh;
    }

    public String getSfyxd() {
        return sfyxd;
    }

    public void setSfyxd(String sfyxd) {
        this.sfyxd = sfyxd;
    }

    public String getXszt() {
        return xszt;
    }

    public void setXszt(String xszt) {
        this.xszt = xszt;
    }

    public Float getFtje() {
        return ftje;
    }

    public void setFtje(Float ftje) {
        this.ftje = ftje;
    }

    public Float getYsjg() {
        return ysjg;
    }

    public void setYsjg(Float ysjg) {
        this.ysjg = ysjg;
    }

    public String getSfzs() {
        return sfzs;
    }

    public void setSfzs(String sfzs) {
        this.sfzs = sfzs;
    }

    public String getSyyxm() {
        return syyxm;
    }

    public void setSyyxm(String syyxm) {
        this.syyxm = syyxm;
    }

    public String getSqrxm() {
        return sqrxm;
    }

    public void setSqrxm(String sqrxm) {
        this.sqrxm = sqrxm;
    }

    public LocalDateTime getZssj() {
        return zssj;
    }

    public void setZssj(LocalDateTime zssj) {
        this.zssj = zssj;
    }

    public Float getDwsl() {
        return dwsl;
    }

    public void setDwsl(Float dwsl) {
        this.dwsl = dwsl;
    }

    public String getSfxs() {
        return sfxs;
    }

    public void setSfxs(String sfxs) {
        this.sfxs = sfxs;
    }

    public String getBy1() {
        return by1;
    }

    public void setBy1(String by1) {
        this.by1 = by1;
    }

    public String getBy2() {
        return by2;
    }

    public void setBy2(String by2) {
        this.by2 = by2;
    }

    public Float getBy3() {
        return this.by3;
    }

    public Float getBy3NonNull() {
        if (this.by3 == null) {
            return 0f;
        } else {
            return this.by3;
        }
    }

    public void setBy3(Float by3) {
        this.by3 = by3;
    }

    public Float getBy4() {
        return by4;
    }

    public void setBy4(Float by4) {
        this.by4 = by4;
    }

    public LocalDateTime getBy5() {
        return by5;
    }

    public void setBy5(LocalDateTime by5) {
        this.by5 = by5;
    }

    public String getSjc() {
        return sjc;
    }

    public void setSjc(String sjc) {
        this.sjc = sjc;
    }

    public String getBy6() {
        return by6;
    }

    public void setBy6(String by6) {
        this.by6 = by6;
    }

    public String getBy7() {
        return by7;
    }

    public void setBy7(String by7) {
        this.by7 = by7;
    }

    public String getBy8() {
        return by8;
    }

    public void setBy8(String by8) {
        this.by8 = by8;
    }

    public Float getBy9() {
        return by9;
    }

    public void setBy9(Float by9) {
        this.by9 = by9;
    }

    public Float getBy10() {
        return by10;
    }

    public void setBy10(Float by10) {
        this.by10 = by10;
    }

    public LocalDateTime getBy11() {
        return by11;
    }

    public void setBy11(LocalDateTime by11) {
        this.by11 = by11;
    }

    public String getTcxmbh() {
        return tcxmbh;
    }

    public void setTcxmbh(String tcxmbh) {
        this.tcxmbh = tcxmbh;
    }

    public String getBy12() {
        return by12;
    }

    public void setBy12(String by12) {
        this.by12 = by12;
    }

    public String getBy13() {
        return by13;
    }

    public void setBy13(String by13) {
        this.by13 = by13;
    }

    public String getPbjsjm() {
        return pbjsjm;
    }

    public void setPbjsjm(String pbjsjm) {
        this.pbjsjm = pbjsjm;
    }

    public Integer getPbxh() {
        return pbxh;
    }

    public void setPbxh(Integer pbxh) {
        this.pbxh = pbxh;
    }

    public String getBy14() {
        return by14;
    }

    public void setBy14(String by14) {
        this.by14 = by14;
    }

    public String getBy15() {
        return by15;
    }

    public void setBy15(String by15) {
        this.by15 = by15;
    }

    public String getBy16() {
        return by16;
    }

    public void setBy16(String by16) {
        this.by16 = by16;
    }

    public String getBy17() {
        return by17;
    }

    public void setBy17(String by17) {
        this.by17 = by17;
    }

    public String getBy18() {
        return by18;
    }

    public void setBy18(String by18) {
        this.by18 = by18;
    }

    public Integer getBy19() {
        return by19;
    }

    public void setBy19(Integer by19) {
        this.by19 = by19;
    }

    public LocalDateTime getBy20() {
        return by20;
    }

    public void setBy20(LocalDateTime by20) {
        this.by20 = by20;
    }

    public String getBy21() {
        return by21;
    }

    public void setBy21(String by21) {
        this.by21 = by21;
    }

    public String getBy22() {
        return by22;
    }

    public void setBy22(String by22) {
        this.by22 = by22;
    }

    public String getBy23() {
        return by23;
    }

    public void setBy23(String by23) {
        this.by23 = by23;
    }

    public String getBy24() {
        return by24;
    }

    public void setBy24(String by24) {
        this.by24 = by24;
    }

    public String getBy25() {
        return by25;
    }

    public void setBy25(String by25) {
        this.by25 = by25;
    }

    // --------------------

    public boolean isMain() {
        return isMain;
    }

    public void setMain(boolean main) {
        isMain = main;
    }

    public long getMainId() {
        return mainId;
    }

    public void setMainId(long mainId) {
        this.mainId = mainId;
    }

    private void computeXj() {
        if ((this.dj == null || this.dj == 0) || (this.sl == null || this.sl == 0)) {
            setXj(0f);
        } else {
            setXj(new BigDecimal(this.dj).multiply(new BigDecimal(this.sl))
                    .setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (obj != null && obj instanceof Wmlsb) {
            Wmlsb w = (Wmlsb) obj;

            return Objects.equals(this.xh, w.xh)
                    && Objects.equals(this.wmdbh, w.wmdbh)
                    && Objects.equals(this.xmbh, w.xmbh)
                    && Objects.equals(this.xmmc, w.xmmc)
                    && Objects.equals(this.tm, w.tm)
                    && Objects.equals(this.dw, w.dw)
                    && Objects.equals(this.sl, w.sl)
                    && Objects.equals(this.dj, w.dj)
                    && Objects.equals(this.xj, w.xj)
                    && Objects.equals(this.pz, w.pz)
                    && Objects.equals(this.tcbh, w.tcbh)
                    && Objects.equals(this.sfyxd, w.sfyxd)
                    && Objects.equals(this.xszt, w.xszt)
                    && Objects.equals(this.ftje, w.ftje)
                    && Objects.equals(this.ysjg, w.ysjg)
                    && Objects.equals(this.sfzs, w.sfzs)
                    && Objects.equals(this.syyxm, w.syyxm)
                    && Objects.equals(this.sqrxm, w.sqrxm)
                    && Objects.equals(this.zssj, w.zssj)
                    && Objects.equals(this.dwsl, w.dwsl)
                    && Objects.equals(this.sfxs, w.sfxs)
                    && Objects.equals(this.by1, w.by1)
                    && Objects.equals(this.by2, w.by2)
                    && Objects.equals(this.by3, w.by3)
                    && Objects.equals(this.by4, w.by4)
                    && Objects.equals(this.by5, w.by5)
                    && Objects.equals(this.sjc, w.sjc)
                    && Objects.equals(this.by6, w.by6)
                    && Objects.equals(this.by7, w.by7)
                    && Objects.equals(this.by8, w.by8)
                    && Objects.equals(this.by9, w.by9)
                    && Objects.equals(this.by10, w.by10)
                    && Objects.equals(this.by11, w.by11)
                    && Objects.equals(this.tcxmbh, w.tcxmbh)
                    && Objects.equals(this.by12, w.by12)
                    && Objects.equals(this.by13, w.by13)
                    && Objects.equals(this.pbjsjm, w.pbjsjm)
                    && Objects.equals(this.pbxh, w.pbxh)
                    && Objects.equals(this.by14, w.by14)
                    && Objects.equals(this.by15, w.by15)
                    && Objects.equals(this.by16, w.by16)
                    && Objects.equals(this.by17, w.by17)
                    && Objects.equals(this.by18, w.by18)
                    && Objects.equals(this.by19, w.by19)
                    && Objects.equals(this.by20, w.by20)
                    && Objects.equals(this.by21, w.by21)
                    && Objects.equals(this.by22, w.by22)
                    && Objects.equals(this.by23, w.by23)
                    && Objects.equals(this.by24, w.by24)
                    && Objects.equals(this.by25, w.by25);
//                    && Objects.equals()

//            w.setMain(false);
//            w.setMainId(0L);

        } else {
            return super.equals(obj);
        }
    }

    public static String toQuerySql(String wmdbh) {
        return String.format(Locale.CHINA, "SELECT xh, wmdbh, xmbh, xmmc, tm, dw, sl, dj, " +
                "xj, pz, tcbh, sfyxd, xszt, ftje, ysjg, sfzs, syyxm, sqrxm, zssj, dwsl, sfxs, " +
                "by1, by2, by3, by4, by5, sjc, by6, by7, by8, by9, by10, by11, tcxmbh, by12, by13, " +
                "pbjsjm, pbxh, by14, by15, by16, by17, by18, by19, by20, by21, by22, by23, by24, " +
                "by25 FROM WMLSB WHERE WMDBH = '%s'|", wmdbh);
    }
}
